package com.jt.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.pojo.ItemCat;
import com.jt.vo.EasyUITree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
private ItemCatMapper itemCatMapper;

    @Override
    public String findItemCatName(Long itemCatId) {
        return itemCatMapper.selectById(itemCatId).getName();
    }

    @Override
    public List<EasyUITree> findItemCatList(Long parentId) {
        //1，准备返回值数据
        List<EasyUITree> treeList=new ArrayList<>();
        //，返回值的数据从哪来？vo 转化  POJO数据

        //2,实现数据库的查询
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent_Id", parentId);
        List<ItemCat>catList=itemCatMapper.selectList(queryWrapper);
        //3，实现数据的转化  catList转化为treeList
        for (ItemCat itemCat:catList){
            long id=itemCat.getId();
            String text=itemCat.getName();
            //判断是父集  应该闭合closed  如果不是  则open
            String state=itemCat.getIsParent()?"closed":"open";
            EasyUITree easyUITree=new EasyUITree(id,text,state);
            treeList.add(easyUITree);
        }
        return treeList;
    }
}
